﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XmlParse
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"..\..\catalog.xml");
            XmlNode rootNode = doc.DocumentElement;
            //Dictionary<string, int> dic = new Dictionary<string, int>();
            //foreach (XmlNode node in rootNode.ChildNodes)
            //{

            //    if (dic.ContainsKey(node["artist"].InnerText))
            //    {
            //        dic[node["artist"].InnerText]++;

            //    }
            //    else
            //    {
            //        dic.Add(node["artist"].InnerText,1);
            //    }

            //   }
            //foreach (var item in dic)
            //{
            //    Console.WriteLine(item.Key+" "+item.Value);
            //}

            //2. Implement the previous using XPath.

            Dictionary<string, int> dic = new Dictionary<string, int>();
            using (XmlTextReader reader = new XmlTextReader(@"..\..\catalog.xml"))
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) &&
                        (reader.Name == "artist"))
                    {
                        string currReader = reader.ReadElementString();
                        if (dic.ContainsKey(currReader))
                        {
                            dic[currReader]++;
                        }
                        else
                        {
                            dic.Add(currReader, 1);
                        }
                    }
                }
            }
            foreach (var item in dic)
            {
                Console.WriteLine(item.Key + " " + item.Value);
            }
        }
    }
}
